<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Register extends CI_Controller {

    public function index()
    {

        $this->load->view('login/register.html');
    }
    public function home(){

             $data=$this->input->post();
            if(!$data['phone']){
                echo "<script type='text/javascript'> alert('账号不能为空');location.href = '/index.php/register/index';</script>";
                return;
            }
             if(!$data['password']){
                 echo "<script type='text/javascript'> alert('密码不能为空');location.href = '/index.php/register/index';</script>";
                 return;
             }
             $data['password']=md5($data['password']);
             $date=$this->db->where('phone',$data['phone'])->get('user')->row_array();//调用model里的方法完成验证
             if($date){
                 echo "<script type='text/javascript'> alert('用户名以经存在'); location.href = '/index.php/register/index';</script>";
                 return;
             }else{
                  $insert=array(
                      'phone'=>$data['phone'],
                      'password'=> $data['password'],
                      'create_time'=>time()
                  );
                 $this->db->insert('user',$insert);//向数据库添加用户
                 $id=$this->db->insert_id();
                 $fid=array(
                     'fid'=>$data['fid'],
                     'user_id'=> $id,
                     'create_time'=>time()
                 );
                 $this->db->insert('relationship',$fid);//向数据库添加用户信息
                 echo "<script type='text/javascript'> alert('注册成功'); location.href = '/index.php/login/index';</script>";
                 return;
             }
//             var_dump($date);
//             die();



    }
}